<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>部门列表</title>
    <script type="text/javascript">
      var newDepartmentWin;
      var editDepartmentWin;
      var departmentId;
      var departmentDS;
      Ext.onReady(function(){
        //部门设置---------------------------------------------------------
        //生成有关部门的Grid
        departmentDS = Ext.create('Ext.data.Store', {
          proxy: new Ext.data.HttpProxy({
            url: '/department/getAllDepartments'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }, {
              name: 'description'
            }])
        });
        var departmentSM = Ext.create('Ext.selection.CheckboxModel');
        var departmentCM = Ext.create('Ext.grid.column.Column',[Ext.create('Ext.grid.RowNumberer'),departmentSM,{
            header: 'id',
            dataIndex: 'id',
            width: 40
          }, {
            header: '编号',
            dataIndex: 'number',
            width:50
          }, {
            header: '名称',
            dataIndex: 'name',
            width:180
          }, {
            header: '描述',
            dataIndex: 'description',
            width:230
          }]);
        var departmentGrid =  Ext.create('Ext.grid.Panel', {
          store: departmentDS,
          columns : departmentCM,
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          width: screenWidth-165,
          height: screenHeight-285,
          iconCls: 'icon-grid',
          //collapsible: true,
          //animCollapse: false,
          //frame: true,
          //title: '部门列表',
          bbar: Ext.create('Ext.toolbar.Paging', {
            pageSize: 10,
            store: departmentDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: [{
              text: '添加',
              iconCls: 'addItem',
              handler : function(){
                newDepartmentWin = Ext.create('Ext.window.Window', {
                  layout: 'fit',
                  width:318,
                  height:220,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加部门',
                  autoLoad: {
                    url: "/department/newDepartment",
                    scripts: true
                  }
                });
                newDepartmentWin.on("destroy",function(){
                  departmentDS.load();
                });
                newDepartmentWin.resizable = false;
                newDepartmentWin.show();
              }
            },'-',{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = departmentGrid.getSelectionModel().getSelection();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  //把表单添加到窗口中
                  departmentId = records[0].get('id');
                  editDepartmentWin = Ext.create('Ext.window.Window', {
                    title: '编辑部门',
                    layout:'fit',
                    width:318,
                    height:220,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/department/editDepartment?id=" + departmentId,
                      scripts: true
                    }
                  });
                }
                editDepartmentWin.on("destroy",function(){
                  departmentDS.load();
                });
                editDepartmentWin.resizable = false;
                editDepartmentWin.show();
              }
            },'-',{
              text: '删除',
              iconCls: 'remove',
              handler : function(){
                var records = departmentGrid.getSelectionModel().getSelection();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  //把表单添加到窗口中
                  departmentId = records[0].get('id');
                  departmentName = records[0].get('name');
                  Ext.MessageBox.confirm("提示","你确认删除 \""+departmentName+"\" 吗？",function(btn){
                    if(btn=="yes"){
                      Ext.Ajax.request({
                        url : '/department/destroy?id=' + departmentId,
                        success : function (result, request) {
                          departmentDS.load();
                        },
                        failure : function (result, request){
                        },
                        method : 'GET'
                      });
                    }else{
                      //alert("no");
                    }
                  });
                }
              }
            }]
        });
        departmentGrid.render('department_list');
      })
    </script>
  </head>
  <body>
    <div id="department_list"></div>
  </body>
</html>
